package org.gangcai.mac.shop.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wuhenzhizao.titlebar.utils.AppUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.activity.ShopDetailActivity;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.IndexBean;
import org.gangcai.mac.shop.bean.UpdateShopData;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.rxlife.RxFragment;
import org.gangcai.mac.shop.utils.PromptDialogUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import me.leefeng.promptlibrary.PromptDialog;

/**
 * 商品界面
 */
public class ShopFragment extends RxFragment implements MultiItemTypeAdapter.OnItemClickListener, OnRefreshListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    Unbinder unbinder;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.main_content)
    CoordinatorLayout mainContent;
    ArrayList<IndexBean.InfoBean> datalist = new ArrayList<>();
    @BindView(R.id.billTitle)
    TextView billTitle;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.titleBar)
    View titleBar;
    @BindView(R.id.increaseiCredit)
    TextView increaseiCredit;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String mParam1;
    private String mParam2;
    private CommonAdapter<IndexBean.InfoBean> adapter;
    private PromptDialog prompDialog;


    public ShopFragment() {
    }

    public static ShopFragment newInstance(String param1, String param2) {
        ShopFragment fragment = new ShopFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        unbinder = ButterKnife.bind(this, view);
        ((LinearLayout.LayoutParams) titleBar.getLayoutParams()).height = AppUtils.getStatusBarHeight(this.getContext());
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        prompDialog = PromptDialogUtils.getPrompDialog(getActivity());
        initView();
        loadData();
    }

    private void loadData() {
        LogUtils.e("loadUserFragment");
        RetrofitManager.getInstance().create(AppAPI.class).index(SPUtils.getInstance().getString("uid"))
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<IndexBean>bindToLifecycle())
                .subscribe(new CommonObserver<IndexBean>(refreshLayout) {
                    @Override
                    public void onSuccess(IndexBean value) {
                        if (value.getCode() == 101) {
                            money.setText(value.getQuota_present());
                            datalist.clear();
                            datalist.addAll(value.getInfo());
                            adapter.notifyDataSetChanged();
                        }
                    }


                });
    }

    private void initView() {
        ((LinearLayout.LayoutParams) billTitle.getLayoutParams()).topMargin = AppUtils.getStatusBarHeight(getContext());
        adapter = new CommonAdapter<IndexBean.InfoBean>(this.getContext(), R.layout.fragment_shop_recyclerview_item, datalist) {
            @Override
            protected void convert(ViewHolder holder, IndexBean.InfoBean o, int position) {

                Glide.with(ShopFragment.this).load(Constant.IMAGE_PATH + o.getImg()).into((ImageView) holder.getView(R.id.squareIv));
                holder.setText(R.id.goodMoney, o.getMoney());
                holder.setText(R.id.goodTitle, o.getGoods_name());
            }

        };
        recyclerview.setLayoutManager(new GridLayoutManager(this.getContext(), 2));
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("id", datalist.get(position).getId());
        ActivityUtils.startActivity(bundle, this.getActivity(), ShopDetailActivity.class);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @OnClick(R.id.increaseiCredit)
    public void onViewClicked() {

        ToastUtils.showLong("请保持良好信用消费,平台根据信用适当提高额度");
    }

    @Subscribe
    public void onEvent(UpdateShopData data) {
        loadData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        loadData();
    }
}
